package cn.amebius.avoice.controller.student;

/**
 * @author 阿伟
 * @Date 2022/10/15 18:47
 */

import cn.amebius.avoice.controller.Result;
import cn.amebius.avoice.pojo.users.Student;
import cn.amebius.avoice.service.StudentService;
import cn.amebius.avoice.util.CodeUtil;
import cn.amebius.avoice.util.MessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Map;
import java.util.Random;

@RequestMapping("/api/user/student")
@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    private Map<String,String> studentCode;

    /**
     * 用户登录的请求
     * @param student 用户实体类
     * @param msg 请求msg
     * @throws IOException IO流错误
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result studentLogin(Student student, String msg) throws IOException {
        System.out.println("student = " + student);
        Result result = new Result();
        boolean isLogin = studentService.studentLogin(student);
        boolean isExist = studentService.studentExist(student);
        if (isLogin) {
            result.setCode(CodeUtil.STUDENT_LOGIN_SUCCESS);
            result.setMsg(MessageUtil.STUDENT_LOGIN_SUCCESS);
        } else {
            if (isExist) {
                result.setCode(CodeUtil.STUDENT_LOGIN_FAIL_PASSWORD);
                result.setMsg(MessageUtil.STUDENT_LOGIN_FAIL_PASSWORD);
            } else {
                result.setCode(CodeUtil.STUDENT_LOGIN_FAIL_EXIST);
                result.setMsg(MessageUtil.STUDENT_LOGIN_FAIL_EXIST);
            }
        }
        return result;
    }

    /**
     * 用户注册的请求
     * @param student 用户实体类
     * @param msg 请求msg
     * @throws IOException IO流错误
     */
    @RequestMapping(value = "/register",method = RequestMethod.PUT)
    @ResponseBody
    public Result studentRegister(Student student, String msg) throws IOException {
        Result result = new Result();
        boolean isExist = studentService.studentExist(student);
        if (isExist) {
            result.setCode(CodeUtil.STUDENT_REGISTER_FAIL_EXIST);
            result.setMsg(MessageUtil.STUDENT_REGISTER_FAIL_EXIST);
        } else {
            if (studentService.studentRegister(student)) {
                result.setCode(CodeUtil.STUDENT_REGISTER_SUCCESS);
                result.setMsg(MessageUtil.STUDENT_REGISTER_SUCCESS);
            } else {
                result.setCode(CodeUtil.STUDENT_REGISTER_FAIL_NULL);
                result.setMsg(MessageUtil.STUDENT_REGISTER_FAIL_NULL);
            }
        }
        return result;
    }


    /**
     * 用户忘记密码
     * 模拟验证码生成，存储在服务端
     * 手机号为key，验证码为value
     */
    @RequestMapping(value = "/forget/code",method = RequestMethod.GET)
    @ResponseBody
    public Result studentForgetGetCode(Student student){
        Result result = new Result();
        StringBuffer code = new StringBuffer();
        Random random = new Random();
        while (code.length() <= 6) {
            code.append(random.nextInt(10));
        }
        studentCode.put(student.getTel(),code.toString());
        result.setCode(CodeUtil.STUDENT_FORGET_CODE);
        result.setMsg(MessageUtil.STUDENT_FORGET_CODE);
        return result;
    }

    /**
     * 用户忘记密码
     * 验证码验证
     */

    @RequestMapping(value = "/forget/code",method = RequestMethod.POST)
    @ResponseBody
    public Result studentForgetCheckCode(Student student,String code){
        Result result = new Result();
        String mapCode = studentCode.get(student.getTel());
        if (mapCode.equals(code)) {
            result.setCode(CodeUtil.STUDENT_FORGET_CODE_SUCCESS);
            result.setMsg(MessageUtil.STUDENT_FORGET_CODE_SUCCESS);
            studentCode.remove(student.getTel());
        } else {
            result.setCode(CodeUtil.STUDENT_FORGET_CODE_FAIL);
            result.setMsg(MessageUtil.STUDENT_FORGET_CODE_FAIL);
        }
        return result;
    }

    /**
     * 用户忘记密码
     * 发送student找回密码
     */
    @RequestMapping(value = "/forget",method = RequestMethod.PUT)
    @ResponseBody
    public Result studentForget(Student student){
        Result result = new Result();
        boolean isExist = studentService.studentExist(student);
        if (isExist) {
            result.setCode(CodeUtil.STUDENT_FORGET_FAIL_EXIST);
            result.setMsg(MessageUtil.STUDENT_FORGET_FAIL_EXIST);
        } else {
            if (studentService.studentForget(student)) {
                result.setCode(CodeUtil.STUDENT_FORGET_SUCCESS);
                result.setMsg(MessageUtil.STUDENT_FORGET_SUCCESS);
            } else {
                result.setCode(CodeUtil.STUDENT_FORGET_FAIL_NULL);
                result.setMsg(MessageUtil.STUDENT_FORGET_FAIL_NULL);
            }
        }
        return result;
    }

}
